<?php
/**
 * Created by PhpStorm.
 * User: 9ey
 * Date: 2019/7/31
 * Time: 0:08
 */

namespace App\Service;

use AlibabaCloud\Client\AlibabaCloud;
use App\Utils\Output;
use Illuminate\Support\Facades\Log;

/**
 * 消息
 * Class SmsService
 * @package App\Service
 */
class SmsService
{
    const ACCESS_KEY_ID = "LTAI67g2bgdCHzWp";
    const ACCESS_SECRET = "CqqeuGU67T6SvNepGBOTHMacWUtHpU";

    /**
     * 发送验证码
     * @param string $mobile 手机号
     * @param string $code 验证码
     * @return  Output
     */
    function sendVerificationCode($mobile, $code)
    {
        return $this->sendMsg($mobile,"SMS_171670459",array('code' => $code));
    }

    /**
     * 发送验证码
     * @param string $mobile 手机号
     * @param $phone
     * @param $time
     * @param $address
     * @return  Output
     */
    function sendOrderTip($mobile, $phone,$time,$address)
    {
        return $this->sendMsg($mobile,"SMS_174585080",array(
            'phone' => $phone,
            'time' => $time,
            'address' => $address
        ));
    }

    function sendMsg($mobile,$tmpCode,$params){
        try {
            AlibabaCloud::accessKeyClient(self::ACCESS_KEY_ID, self::ACCESS_SECRET)
                ->regionId('default')// replace regionId as you need
                ->asDefaultClient();
            $result=AlibabaCloud::rpc()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "default",
                        'PhoneNumbers' => $mobile,
                        'SignName' => "安护出行",
                        'TemplateCode' => $tmpCode,
                        'TemplateParam' => json_encode($params)
                    ],
                ])
                ->request()->toArray();
            if($result['Code']=='OK'){
                return Output::success('发送成功');
            }else{
                return Output::fail($result['Message']);
            }
        } catch (\Exception $e) {
            Log::info($e->getMessage() . "\r\n" . $e->getTraceAsString());
            return Output::fail($e->getMessage());
        }
    }
}
